package ManageGameHServer.classFile;

import ManageGameHServer.conf.Setup;
import redis.clients.jedis.Jedis;


import java.util.NoSuchElementException;
import java.util.concurrent.ArrayBlockingQueue;

/**
 * Created by lt on 2017/3/3.
 */
public class NoDBConnectO {
    private static ArrayBlockingQueue<Jedis> ConnectionQueue =new ArrayBlockingQueue<Jedis>(Setup.NoSqlPoolSum);
    public static void Initialization(){
        System.out.println("NOSqlDB_Connect Already initialized .....");
        int row;
        int poolSum=Setup.NoSqlPoolSum;
        for (row=1; row <= poolSum; row++) {
            Jedis jedisLink=new Jedis(Setup.NoSqlUrl,Setup.NoSqlPort);
            jedisLink.auth(Setup.NoSqlPassword);
            jedisLink.select(Setup.NoSqlSelect);
            ConnectionQueue.add(jedisLink);
        }
    }
    public static Jedis getCon(){
        Jedis redConnection;
        try {
            redConnection = ConnectionQueue.remove();
        }catch ( NoSuchElementException NoSuchE){
            Log.errorLog("nosql连接线程不够!!!");
            redConnection=new Jedis(Setup.NoSqlUrl,Setup.NoSqlPort);
            redConnection.auth(Setup.NoSqlPassword);
            redConnection.select(Setup.NoSqlSelect);
        }
        return redConnection;
    }
    public static void redCon(Jedis connection){
        try {
            ConnectionQueue.add(connection);
        }catch (Exception e){
            connection.close();
        }
    }
}
